package com.fs.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class CmdUtils {
	
	private Context context;
	private Handler handler;
	
	public CmdUtils(Context context) {
	 
		//get Context
		this.context=context;
	}
	
	public CmdUtils(Context context,Handler handler) {
		 
		this.context=context;
		this.handler=handler;
		
	}
	
	/**
	 * submit CMD 向服务器提交指令
	 */
	public void submitCmd(String url,String action,String cmd) {
		//封装指令
		RequestParams params = new RequestParams();
		
		//数据请求
		params.addBodyParameter("ACTION", action);//意图
		params.addBodyParameter("CMD",cmd);//表
		
		
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
//				System.out.println("返回结果："+responseInfo.result);//【测试】
//				String result=responseInfo.result;
//				if("200".equals(result)){
					Toast.makeText(context, "操作成功", 0).show();
//				}else {
//					Toast.makeText(context, "操作失败", 0).show();
//				}
					if(handler!=null){
					
						new Thread(){
	
							@Override
							public void run() {
								super.run();
								
								Message msg=handler.obtainMessage();
								msg.what=2;
								handler.sendMessage(msg);
							}
						}.start();
					}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				System.out.println("失败");//【测试】
			}

		});
	}
}
